package edu.mobbuzz.web.components;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.mobbuzz.daf.bean.User;
import edu.mobbuzz.daf.dao.IUserDao;
import edu.mobbuzz.web.base.SimpleBasePage;
import edu.mobbuzz.web.pages.RoomGridPage;
import edu.mobbuzz.web.state.Visit;

public class Login extends SimpleBasePage{
	@Persist("flash")
	private String username;	
	private String password;
	
	@Component
	private Form loginForm;
	
	@Inject
	private IUserDao userDao;
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public IUserDao getUserDao() {
		return userDao;
	}
	Object onSuccessFromLoginForm() {
		Object nextPage = null;
		try {
			User user = (User)userDao.findUserId(getUsername(), getPassword());
			if(user!=null){
				Visit visit = getVisit();
				visit.noteLogIn(user);
				
				nextPage = RoomGridPage.class;
			}
		}catch (Exception e) {
			loginForm.recordError("Wrong Account");
		}
		
		
		return nextPage;
	}
	boolean beforeRender(MarkupWriter writer){
		 return true;
	} 
}
